/**
 * 
 */
package face.unisannio;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import face.unisannio.content.StudentDB;

/**
 * @author Fabio Melillo
 * 
 */
public class MainFeatures extends Activity {
	
	private Spinner labels;
	private StudentDB sdb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		sdb = new StudentDB(MainFeatures.this);
		boolean open = sdb.open();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button bscatta = (Button) findViewById(R.id.bscatta);
		bscatta.setText("Fai l'appello");
		bscatta.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startCamera();
			}
		});

		Button debug = (Button) findViewById(R.id.dump);
		debug.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				File dump=new File(getExternalFilesDir(null), "DBdump.txt");
				try {
					sdb.dump(dump);
//					sdb.restore(dump);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
				// sdb.drop();
				// sdb=new StudentDB(MainFeatures.this);
				// open=sdb.open();
//				ContentValues cv = new ContentValues();
//				cv.put(StudentDB.STUDENT_NOME, "Claudio");
//				cv.put(StudentDB.STUDENT_COGNOME, "Di Cosmo");
//				cv.put(StudentDB.STUDENT_CLASSE, "4E");
//				String selection = StudentDB.STUDENT_NOME + "= ? AND "
//						+ StudentDB.STUDENT_COGNOME + "=? AND "
//						+ StudentDB.STUDENT_CLASSE + "= ?";
//				String[] selectionArgs = { "Claudio", "Di Cosmo", "4E" };
//				Cursor c = sdb.query(null, selection, selectionArgs, null,
//						null, null);
//				startManagingCursor(c);
//				if (c.getCount() > 0) {
//					c.moveToFirst();
//					String id=c.getString(c.getColumnIndex(StudentDB.STUDENT_ID));
//					String[] args={id};
//					int res=sdb.deleteStudent(StudentDB.STUDENT_ID+"=? ", args);
//				} 
//				
//				{
//					long id = sdb.insert(cv);
//					selection = StudentDB.STUDENT_ID + "= ?";
//					String[] selectionArgsNew = { String.valueOf(id) };
//					c = sdb.query(null, selection, selectionArgsNew, null,
//							null, null);
//					// Cursor c=sdb.query(null, null, null, null, null, null);
//					c.moveToFirst();
//					String uid = c.getString(c
//							.getColumnIndex(StudentDB.STUDENT_UID));
//					String label = c.getString(c
//							.getColumnIndex(StudentDB.STUDENT_LABEL));
//					System.out.println(c.getCount()+" "+uid+" "+label+ "ID: "+c.getString(c.getColumnIndex(StudentDB.STUDENT_ID)));
//					// int i=sdb.deleteStudent(null, null);
//					populate();
//				}
			}
		});
		
		Button restore = (Button) findViewById(R.id.restore);
		restore.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				File dump=new File(getExternalFilesDir(null), "DBdump.txt");
				try {
//					sdb.dump(dump);
					sdb.restore(dump);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
		
		labels=(Spinner) findViewById(R.id.spinnerLabels);
		this.populate();
		
		labels.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	if(position>0){
				Toast toast=Toast.makeText(MainFeatures.this, "Hai selezionato l'elemento numero:"+position, Toast.LENGTH_SHORT);
				toast.show();
				}
		    }

		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {
		        // your code here
		    }

		});
		
	}

	private void populate() {
		String[] etichette=sdb.getStringArray(StudentDB.STUDENT_LABEL,null,null,null, null, StudentDB.STUDENT_COGNOME);
//		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter(this,
//		        android.R.layout.simple_spinner_dropdown_item,
//		            etichette);
		ArrayAdapter spinnerArrayAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,etichette);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		labels.setAdapter(spinnerArrayAdapter);
	}

	private void startCamera() {
		System.out.println("START CAMERA");
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(getExternalFilesDir(null),
				FaceRecognitionActivity.PIC_FILENAME);
		Uri fileUri = Uri.fromFile(file); // create a file to save the image
		intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
															// name
		// start the image capture Intent
		this.startActivityForResult(intent,
				FaceRecognitionActivity.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Intent i = new Intent(this, FaceRecognitionActivity.class);
		i.putExtra(FaceRecognitionActivity.CAPTURED, resultCode);
		startActivity(i);
	}


//	@Override
//	public void onAttachedToWindow() {
//	// TODO Auto-generated method stub
//		super.onAttachedToWindow();
//		int i=labels.getSelectedItemPosition();
//		Toast toast=Toast.makeText(this, "Hai selezionato l'elemento numero:"+i, Toast.LENGTH_SHORT);
//		toast.show();
//	}
}
